import time
import csv
import os


from splider.common import CSVUtil
from splider.common import HttpRequest
from splider.common import Progress_Tracker

urlroot = "https://car.autohome.com.cn"


# 车型列表
def get_model():
    current_dir = os.path.abspath('.')
    print(current_dir)

    model_csv_path = "data/model.csv"
    series_csv_path = "data/series.csv"
    point_key ="series_model"

    model_csv_file = os.path.join(current_dir,model_csv_path)
    series_csv_file = os.path.join(current_dir, series_csv_path)
    progress_file = os.path.join(current_dir, "data/progress.txt")

    # 读取当前进度
    Progress_Tracker.load_progress(progress_file)
    # 删除车型中在车系最后位点处已经写入的行(避免重复写入)
    Progress_Tracker.delete_matching_rows(model_csv_path, series_csv_path, point_key,1)

    start_line = Progress_Tracker.get_progress_data()[point_key]



    # 读取并获得所有URL地址，同时记录品牌名称
    with open(series_csv_file, 'r') as scf, open(model_csv_file, 'a', newline='') as mcf:  # 使用追加模式
        writer = csv.writer(mcf)
        reader = csv.reader(scf)
        for i, row in enumerate(reader):
            if i < start_line:
                continue


            splider_model_count = 0    #页面已经爬取的车型数
            # 01:读取整行
            brand = row[0]          #品牌
            seriesName = row[1]    #车系
            model_nums = row[2]    #车型数量
            seriesURL = row[3]     #车系url

            response = HttpRequest.fetch_url(seriesURL)
            if not response:
                print("Failed to fetch the page.")
            else:
                print(f"品牌={brand},车系名称={seriesName},车系url={seriesURL},车型数量={model_nums}")

                sell_or_not_tabs = response.html.find('.tab-nav.border-t-no ul li a')

                #如果有多个tab页可切换，要循环请求
                if (len(sell_or_not_tabs) > 1):
                     for sell_or_not_tab in sell_or_not_tabs:
                        sell_tab_url = '{}{}'.format(urlroot,sell_or_not_tab.attrs['href'])
                        response = HttpRequest.fetch_url(sell_tab_url)
                        splider_model_count = getCurrentTab(writer, response, brand, seriesName,splider_model_count)
                else:
                    #请求当前tab页的数据
                    getCurrentTab(writer, response, brand, seriesName,splider_model_count)

            #flush()才会真正的把数据从内存写到csv文件
            mcf.flush()
            print()
            # 在循环的最后，保存当前进度
            Progress_Tracker.save_progress(progress_file, point_key, i + 1)
            time.sleep(1)


def getCurrentTab(writer,response, brand, seriesName,splider_model_count):

    splider_model_count = getCurrentPage(writer, response, brand, seriesName,splider_model_count)
    pages = response.html.find('.tab-content-item .page a')
    if (len(pages) > 0):
        for page in pages:
            # 上一页和最后一页，第一页除外
            if page == pages[0] or page == pages[1] or page == pages[-1]:
                continue
            page_url = '{}{}'.format(urlroot,page.attrs['href'])
            response = HttpRequest.fetch_url(page_url)
            splider_model_count = getCurrentPage(writer, response, brand, seriesName,splider_model_count)
    return splider_model_count


def getCurrentPage(writer,response, brand, seriesName,splider_model_count):
    # 获取.tab-content-item .interval01-list下的ul，ul下的li
    # li在ul下面
    models = response.html.find('.tab-content-item .interval01-list li')

    for model in models:

        splider_model_count+=1

        # 获取model下的第一个超链接
        model_url = 'http:{}'.format(model.find('a')[0].attrs['href'])

        # 获取model下的第一个超链接里的内容
        model_name = model.find('a')[0].text

        price_guidance = model.find('.interval01-list-guidance div')[0].text

        results = model.find('.interval01-list-related a')

        modelDetail_url = None
        if len(results) > 1:
            modelDetail_url = "{}{}".format(urlroot, results[1].attrs['href'])

        modelConfig_url = None
        if len(results) > 3:
            modelConfig_url = "{}{}".format(urlroot, results[3].attrs['href'])

        CSVUtil.save2csv(writer, brand, seriesName, model_name, model_url, price_guidance, modelDetail_url,
                         modelConfig_url)

        print(f"{splider_model_count}: {model_name},{price_guidance}")

    return splider_model_count


if __name__ == '__main__':
    get_model()
    print("处理结束")


